import {
  GetOrganizationList,
  GetOrganizationListDetail
} from '@/apis/management/organization/organizationManagement';
import { flatTree, dataFilter, dataTreeFilter } from './utils';
import {
  GetJobList,
  GetPosition,
  GetPositionDefault
} from '@/apis/management/organization/positionManagement';
import {
  GetHumanRosterList,
  GetHumanRosterDetail
} from '@/apis/management/human/humanRoster';
import { GetlistData } from '@/apis/management/personnel/baseSetting/TransactionSetting';
import { getPositionHierarchyListById } from '@/apis/management/organization/baseSetting/PositionSystem/index';
import { GetWorkPlaceList } from '@/apis/management/organization/organizationManagement';
import { getPositionSortList } from '@/apis/management/organization/baseSetting/PositionSystem/index';
import {
  GetHumanChangeSetting,
  GetHumanChangeSettingType
} from '@/apis/management/human/baseSetting/humanSetting';
import { GetHumanLabel } from '@/apis/management/human/baseSetting/humanSetting';

// 获取组织详情
export const getOrganizationDetailById = async (id: number) => {
  try {
    const res = await GetOrganizationListDetail({
      id
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取树形的组织数据
export const getTreeDept = async () => {
  try {
    const res = await GetOrganizationList({});
    return res;
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取平级的组织数据
export const getFlatDept = async () => {
  try {
    const res = await GetOrganizationList({});
    return flatTree(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取职位列表信息
export const getPositionList = async () => {
  try {
    const res = await GetPosition({
      pageNumber: 0,
      pageSize: -1
    });
    return res;
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取职位列表信息
export const getPositionFlatList = async () => {
  try {
    const res = await GetPosition({
      pageNumber: 0,
      pageSize: -1
    });
    return flatTree(dataTreeFilter(res.content));
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取职位详情
export const getPositionDetailById = async (id: number) => {
  try {
    const res = await GetPositionDefault({
      id
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取职位类别扁平数据
export const getPositionTypeFlatList = async () => {
  try {
    const { positionCategoryList } = await getPositionSortList();
    return flatTree(positionCategoryList);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取具体职务信息
export const getJobDetail = async (id: number) => {
  try {
    const { content } = await GetJobList({
      pageNumber: 0,
      pageSize: -1
    });
    const temp = content.find((item: any) => item.id === id);
    return temp;
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取花名册全部人员
export const getAllHumanRoster = async () => {
  try {
    const { content } = await GetHumanRosterList({
      pageNumber: 0,
      pageSize: -1,
      typeName: '1,2,3,4,5,6,7,8,9,10,11,12,13,14'
    });
    return content;
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取人员详情
export const getHumanDetail = async (id: number) => {
  try {
    const res = await GetHumanRosterDetail({
      id
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

/* 
  func: 查重函数
  data: 参数
*/
// 校验查重规则
export const validateRules = async (func: any, data: any) => {
  try {
    const res = await func({
      ...data
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 根据公司ID，请求部门下拉数据
export const getDeptListByCompanyId = async (data: any) => {
  try {
    let id = 0;
    for (const item of data) {
      for (const ele of item.controlItems) {
        if (ele.code === 'CA8ABCC1480744557B3014C3454465A7E') {
          id = ele.ctlValue;
        } else if (ele.code === 'CED44AC9966904CD1AB546D309903727C') {
          const res = await GetlistData({ type: 2, id });
          setTimeout(() => {
            ele.selectOptions = res;
          }, 100);
          console.log('========resres============================');
          console.log(res);
          console.log('====================================');
        }
      }
    }
  } catch (error: unknown) {
    console.log(error);
  }
};

// 根据职位ID，请求职位层级下拉数据
export const getPositionLevelByPositionId = async (data: any) => {
  try {
    let positionId = 0; // 职位id
    for (const item of data) {
      for (const ele of item.controlItems) {
        // if (ele.code === 'C14A4E93E33754840ADDD083163AB7C68') {
        //   ele.ctlValue = ele.ctlValue.split(',');
        // }
        if (ele.code === 'C7341027925C943708F3AEF98703ECC47') {
          positionId = ele.ctlValue;
        } else if (ele.code === 'C9A9EF3DFFFC84AAFB97443DEE8E5B8CC') {
          const res = await getPositionHierarchyListById({
            type: 1,
            id: positionId
          });
          setTimeout(() => {
            ele.selectOptions = res;
          }, 100);
        } else if (ele.code === 'C5B3625070EFD4698A20925CE32B31F16') {
          const res = await getPositionHierarchyListById({
            type: 2,
            id: positionId
          });
          setTimeout(() => {
            ele.selectOptions = res;
          }, 100);
        }
      }
    }
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取全部工作地址
export const getAllWorkAddress = async () => {
  try {
    const { content } = await GetWorkPlaceList({
      pageNumber: 0,
      pageSize: -1
    });
    return content;
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取全部异动类型
export const getAllHumanChangeSettingType = async () => {
  try {
    const res = await GetHumanChangeSettingType({
      name: '1,2'
    });
    return res;
  } catch (error: unknown) {
    console.log(error);
  }
};

// 获取全部人才标签的子标签(扁平化)
export const getAllHumanSubTags = async () => {
  try {
    const { perTagTypeList } = await GetHumanLabel({});
    const temp: any = [];
    perTagTypeList.forEach((el: any) => {
      el.subTag.forEach((el2: any) => {
        temp.push(el2);
      });
    });
    return Promise.resolve(temp);
  } catch (error: unknown) {
    console.log(error);
  }
};
